package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.exception.CustomerException;
import com.example.mapper.AdminMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    public List<Admin> selectAll() {
        return adminMapper.selectAll(null);
    }


    public PageInfo<Admin> selectPage(Integer pageNum, Integer pageSize, String name) {
        // 开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> list = adminMapper.selectAll(name);
        return PageInfo.of(list);
    }
    public void add(Admin admin){
        // 判断账号是否重复
        if(adminMapper.selectByUsername(admin.getUsername())!=0){
            throw new CustomerException("账号重复");
        }
        // 密码为空时，默认密码为123456
        if(StrUtil.isBlank(admin.getPassword())){
            admin.setPassword("123456");
        }
        if(StrUtil.isBlank(admin.getName())){
            admin.setUsername(admin.getUsername());
        }
        if(StrUtil.isBlank(admin.getAvatar())){
            admin.setAvatar("http://svgzmkt0s.hn-bkt.clouddn.com/avatar/admin/tx.jpg");
        }
        adminMapper.insert(admin);
    }

    public void update(Admin admin) {
//        if(adminMapper.selectByUsername(admin.getUsername())!=0){
//            throw new CustomerException("账号重复");
//        }
        System.out.println("admin====>"+admin);
        adminMapper.updateById(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    public void deleteBatch(List<Admin> list) {
        for (Admin admin : list) {
            this.deleteById(admin.getId());
        }
    }

    public Admin login(String username, String password) {
        // 创建token并返回给前端
        Admin admin = adminMapper.selectByUsernameAndPassword(username, password);
        String token=TokenUtils.createToken(admin.getId() + "-" + "ADMIN",admin.getPassword());
        admin.setToken(token);
        return admin;
    }

    public void register(Admin admin) {
        this.add(admin);
    }

    public Account selectById(String userId) {
        return adminMapper.selectById(userId);
    }

    public void updatePassword(Account account) {
//        adminMapper.updateById(account);
        // 校验原密码是否正确
        Account user = TokenUtils.getCurrentUser();
        System.out.println("user=====>"+user);
        if(!user.getPassword().equals(account.getPassword())){
            throw new CustomerException("原密码错误");
        }
        // 修改密码
        Account admin = adminMapper.selectById(account.getId().toString());
        admin.setPassword(account.getNewPassword());
        adminMapper.updateById(admin);

    }
}